/**
 * Copyright (C), 2023-2033
 */
package com.goaly.toolwin;

import com.goaly.action.MyDumbAwareAction;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentFactory.SERVICE;
import com.intellij.ui.content.ContentManager;
import org.jetbrains.annotations.NotNull;

/**
 * 代码助手工具窗口<br> 在 IDEA 如果想展示自己的窗体，则需要开发对应的 ToolWindowFactory 实现类，这样才可以展示你的内容。
 *
 * @author: lys
 * @date: 2023/6/11 18:01
 */
public class CodeHelperWinFactory implements ToolWindowFactory {

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        // 为空时隐藏
        toolWindow.setToHideOnEmptyContent(true);
        ContentFactory contentFactory = SERVICE.getInstance();
        Content firstPanel = contentFactory.createContent(new MyPanel(false), "FirstPanel", false);

        ContentManager contentManager = toolWindow.getContentManager();
        // 添加内容
        contentManager.addContent(firstPanel, 0);

    }

    /**
     * 自定义面板
     */
    class MyPanel extends SimpleToolWindowPanel {

        public MyPanel(boolean vertical) {
            super(vertical);
            DefaultActionGroup group = new DefaultActionGroup();
            group.add(new MyDumbAwareAction("Login1"));
            group.add(new MyDumbAwareAction("Login2"));
            group.add(new MyDumbAwareAction("Login3"));

            ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("MyToolbar", group, false);
            toolbar.setTargetComponent(this);
            setToolbar(toolbar.getComponent());
        }
    }
}
